package com.onpositive.knowledge.model.values;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import com.onpositive.semantic.model.api.property.IProperty;

public class MergeIdenticalTransformer implements IValueTransformer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6524830055147646294L;
	public static final String ID = "merge-identical-transformer";

	@Override
	public DescribedValue[] adjust(Object object, IProperty prop, DescribedValue... original) {

		if (original == null)
			return null;

		LinkedHashMap<Object, ArrayList<DescribedValue>> dm = new LinkedHashMap<Object, ArrayList<DescribedValue>>();

		for (DescribedValue d : original)
		{
			if (d.value == null)
				continue ;
			
			ArrayList<DescribedValue> arrayList = dm.get(d.value);
			if (arrayList == null) {
				arrayList = new ArrayList<DescribedValue>();
				dm.put(d.value, arrayList);
			}
			arrayList.add(d);			
		}
		
		ArrayList<DescribedValue> vl = new ArrayList<DescribedValue>();
		for ( ArrayList<DescribedValue> q : dm.values() )
			vl.add(DescribedValue.chooseBest(q));
		
		return vl.toArray(new DescribedValue[vl.size()]);
	}

	@Override
	public String getId() {
		return ID;
	}

}